package implementation.search;


import anaydis.search.SymbolTable;

import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

public class TextIndex implements anaydis.search.text.TextIndex {
    SymbolTable<String, List<Integer>> table;

   public TextIndex(SymbolTable<String, List<Integer>> table) {
        this.table = table;
    }

    public void buildIndex(String s, int n) {
        table.clear();
        StringTokenizer stringTokenizer = new StringTokenizer(s);
        String word;
        int position = 0;
        while (stringTokenizer.hasMoreTokens() && n>0) {
            word = stringTokenizer.nextToken();
            if (table.containsKey(word)) {
                (table.get(word)).add(position);
            } else {
                List<Integer> list = new LinkedList<Integer>();
                list.add(position);
                table.put(word, list);
                n--;
            }
            position++;
        }

    }

    public void buildIndex(String s) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<Integer> search(String s) {
        return table.get(s);
    }

}
